package es.aa.android.cartomatic.appview;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
import es.aa.android.cartomatic.R;
import es.aa.android.cartomatic.appview.adapter.DeckAdapter;
import es.aa.android.cartomatic.domain.Deck;
import es.aa.android.cartomatic.domain.Options;
import es.aa.android.cartomatic.implementations.da.SQLiteAccess;
import es.aa.android.cartomatic.implementations.service.NavigationService;
import es.aa.android.cartomatic.interfaces.da.ISQLiteAccess;
import es.aa.android.cartomatic.interfaces.service.INavigationService;

public class OptionsView extends FragmentActivity {

	private ISQLiteAccess sqliteAccess;
	private INavigationService navService;
	
	private List<Deck> decksList;
	
	private CheckBox allowSeeDeck;
	private CheckBox allowShuffle;
	private EditText numCounts;
	private EditText numPlayers;
	
	private RadioButton selectOneDeck;
	private RadioButton selectMultipleDeck;
	
	private LinearLayout decksSelected;
	private Button done;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.options);

		init();

	}

	private void init() {

		navService = new NavigationService();
		sqliteAccess = new SQLiteAccess();
		decksList = sqliteAccess.readDeckLibrary(this);
		
		allowSeeDeck = (CheckBox) findViewById(R.id.allowSeeDeck);
		allowShuffle = (CheckBox) findViewById(R.id.allowShuffle);
		numCounts = (EditText) findViewById(R.id.numCounts);
		numPlayers = (EditText) findViewById(R.id.numPlayers);
		
		selectOneDeck = (RadioButton) findViewById(R.id.selectOneDeck);
		selectMultipleDeck = (RadioButton) findViewById(R.id.selectMultipleDeck);
		
		decksSelected = (LinearLayout) findViewById(R.id.llDecksSelected);
		decksSelected.addView(getNewSpinner());
		
		done = (Button) findViewById(R.id.done);
		initListeners();
	}

	private void initListeners() {
		
		selectOneDeck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					decksSelected.removeAllViews();
					decksSelected.addView(getNewSpinner());
				}
			}
		});
		
		selectMultipleDeck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					decksSelected.removeAllViews();
					showDecksForPlayers();
				}
			}
		});
		
		numPlayers.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) { }
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) { }
			
			@Override
			public void afterTextChanged(Editable s) {
				showDecksForPlayers();
			}
		});
		
		done.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				submitOptions();
			}
		});
		
	}
	
	private Spinner getNewSpinner() {

		Spinner spinner = new Spinner(this);
		DeckAdapter adapter = new DeckAdapter(this, decksList, false, false);
		spinner.setAdapter(adapter);
		return spinner;
		
	}
	
	private void showDecksForPlayers() {
		try {
			if(selectMultipleDeck.isChecked()) {
				decksSelected.removeAllViews();
				int count = Integer.valueOf(numPlayers.getText().toString());
				for(int i = 0; i < count; i++) {
					decksSelected.addView(getNewSpinner());
				}
			}
		} catch (NumberFormatException e) {
			
		}
		
	}
	
	private int[] getIdDecks(int numPlayers) {
		int[] ids = new int[numPlayers];
		for (int i = 0; i < decksSelected.getChildCount(); i++) {
			Deck deckSelected = (Deck) ((Spinner) decksSelected.getChildAt(i)).getSelectedItem();
			if(deckSelected instanceof Deck) {
				Toast.makeText(getApplicationContext(), String.valueOf(deckSelected.getId()), Toast.LENGTH_LONG).show();
				ids[i] = deckSelected.getId();
			}
		}
		return ids;
	}

	private void submitOptions() {
		
		int countPlayers = Integer.valueOf(numPlayers.getText().toString());
		
		Options options = new Options();
		options.setCollection(allowSeeDeck.isChecked());
		options.setShuffle(allowShuffle.isChecked());
		options.setCount(Integer.valueOf(numCounts.getText().toString()));
		options.setNum_Players(countPlayers);
		if(selectOneDeck.isChecked()) {
			options.setNum_Decks(1);
		} else {
			options.setNum_Decks(countPlayers);
		}
		options.setDecksIDS(getIdDecks(countPlayers));
		
		navService.navigate(this, MainView.class, options);
	}

}
